﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SuperMarketSystem
{
    public partial class ProviderForm : Form
    {
        public ProviderForm()
        {
            InitializeComponent();
            fillProviders();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            fillProviders();
        }
        private void fillProviders()//填充供应商信息表格
        {
            Providers.Items.Clear();
            String[] tbStrings = { "providerID", "providername", "providermail", "provideraddress", "providerphone", "providerman", "providermanphone" };
            SqlConnection connectionLogin = MySqlServerUtils.getMySqlServerConnection();
            String smCommand = "select * from providers";
            SqlCommand commandLogin = new SqlCommand(smCommand, connectionLogin);
            SqlDataReader smdr = commandLogin.ExecuteReader();
            while (smdr.Read())
            {
                ListViewItem listToAdd = new ListViewItem();
                listToAdd.Text = smdr[tbStrings[0]].ToString();
                for (int i = 1; i < Providers.Columns.Count; i++)
                {
                    listToAdd.SubItems.Add(smdr[tbStrings[i]].ToString());
                }
                Providers.Items.Add(listToAdd);
            }
            try
            {
                connectionLogin.Close();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.StackTrace);
            }
        }

        private void btnSearchByID_Click(object sender, EventArgs e)//按照ID进行查询
        {
            SearchLimit searchLimit = new SearchLimit("ID");
            searchLimit.ShowDialog();
            int limitID = -1;
            if (MyOtherUtils.checkInt(searchLimit.returnByName()))
            {
                limitID = searchLimit.returnByID();
            }
            if (limitID != -1)
            {
                Providers.Items.Clear();
                String[] tbStrings = { "providerID", "providername", "providermail", "provideraddress", "providerphone", "providerman", "providermanphone" };
                String sql = String.Format("select * from providers where providerID={0}", limitID);
                SqlConnection connectionLogin = MySqlServerUtils.getMySqlServerConnection();
                SqlCommand commandLogin = new SqlCommand(sql, connectionLogin);
                SqlDataReader smdr = commandLogin.ExecuteReader();
                while (smdr.Read())
                {
                    ListViewItem listToAdd = new ListViewItem();
                    listToAdd.Text = smdr[tbStrings[0]].ToString();
                    for (int i = 1; i < Providers.Columns.Count; i++)
                    {
                        listToAdd.SubItems.Add(smdr[tbStrings[i]].ToString());
                    }
                    Providers.Items.Add(listToAdd);
                }
                try
                {
                    connectionLogin.Close();
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.StackTrace);
                }
            }
        }

        private void btnSearchByName_Click(object sender, EventArgs e)//按照名称进行模糊查询
        {
            SearchLimit searchLimit = new SearchLimit("Name");
            searchLimit.ShowDialog();
            String limitName = searchLimit.returnByName();
            if (!limitName.Equals(""))
            {
                Providers.Items.Clear();
                String[] tbStrings = { "providerID", "providername", "providermail", "provideraddress", "providerphone", "providerman", "providermanphone" };
                String sql = String.Format("select * from providers where providername like '{0}'", "%" + limitName + "%");
                SqlConnection connectionLogin = MySqlServerUtils.getMySqlServerConnection();
                SqlCommand commandLogin = new SqlCommand(sql, connectionLogin);
                SqlDataReader smdr = commandLogin.ExecuteReader();
                while (smdr.Read())
                {
                    ListViewItem listToAdd = new ListViewItem();
                    listToAdd.Text = smdr[tbStrings[0]].ToString();
                    for (int i = 1; i < Providers.Columns.Count; i++)
                    {
                        listToAdd.SubItems.Add(smdr[tbStrings[i]].ToString());
                    }
                    Providers.Items.Add(listToAdd);
                }
                try
                {
                    connectionLogin.Close();
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.StackTrace);
                }
            }
        }
    }
}
